/**
 * Created by Administrator on 2016/9/18.
 */

define(function (require, exports) {
    var ajaxUtil = require('util/ajaxUtil').ajaxUtil;
    var logUtil = require('util/logUtil').logUtil;
    var commonUtil = require('util/commonUtil').commonUtil;
    var validationUtil = require('util/validationUtil').validationUtil;

    var controls = {};
    controls.app_coupon_discount_list_panel = $('#app_coupon_discount_list_panel');

    var urls = {};
    urls.list = '/admin/promote/coupon-list';
    urls.create = '/admin/promote/coupon-create';


    function init() {
        loadModelData();
        bindEvent();
    }

    function bindEvent() {
        controls.app_coupon_discount_list_panel.find('button:eq(0)').bind('click', function () {
            var batch_code = $.trim(controls.app_coupon_discount_list_panel.find('input[type=text]:eq(0)').val());
            var discount = $.trim(controls.app_coupon_discount_list_panel.find('input[type=text]:eq(1)').val());
            var num = $.trim(controls.app_coupon_discount_list_panel.find('input[type=text]:eq(2)').val());

            if (!batch_code) return commonUtil.toast('批号不能为空');
            if (!discount) return commonUtil.toast('折扣不能为空');
            if (!num) return commonUtil.toast('数量不能为空');

            if (!validationUtil.isWholeNumber(batch_code)) return commonUtil.toast('批号只能是正整数');
            if (!validationUtil.isDiscount(discount)) return commonUtil.toast('折扣格式不正确');
            if (!validationUtil.isWholeNumber(num)) return commonUtil.toast('数量只能是正整数');


            var args = {
                num: num,
                discount: discount,
                batch_code: batch_code,
                type: 1
            };

            ajaxUtil.doAjaxPost(urls.create, args).done(function (ret) {
                if (ret.status == 0) {
                    commonUtil.toast('生成成功');
                    refresh();
                } else {
                    commonUtil.toast(ret.msg);
                }
            });

        });

        controls.app_coupon_discount_list_panel.find('button:eq(1)').bind('click', function () {
            refresh();
        });

        controls.app_coupon_discount_list_panel.find('button:eq(2)').bind('click', function () {
            var batch_code = $.trim(controls.app_coupon_discount_list_panel.find('input[type=text]:eq(3)').val());
            window.location.href = '/admin/promote/export-xls?type=1&batch_code=' + batch_code
        });

    }

    function refresh(data) {
        data = data || {};

        data.batch_code = $.trim(controls.app_coupon_discount_list_panel.find('input[type=text]:eq(3)').val());
        var page = $("#app_coupon_discount_list_grid").getGridParam('page'); // current page
        var rows = $("#app_coupon_discount_list_grid").getGridParam('rows'); // rows

        $("#app_coupon_discount_list_grid").jqGrid('setGridParam', {
            postData: data,
            page: page,
            rows: rows
        }).trigger("reloadGrid")
    }

    function loadModelData() {
        $("#app_coupon_discount_list_grid").jqGrid({
            colModel: [
                {
                    label: '批号',
                    name: 'batch_code',
                    align: 'center',
                },
                {
                    label: '序号',
                    name: 'idx',
                    align: 'center',
                },
                {
                    label: '激活码',
                    name: 'code',
                    align: 'center',
                },
                {
                    label: '折扣',
                    name: 'discount',
                    align: 'center',
                },

                {
                    label: '生成时间',
                    name: 'create_time',
                    align: 'center',
                    formatter: function (cellValue, options, rowObject) {
                        return commonUtil.getTimeString(cellValue);
                    },
                },
                {
                    label: '状态',
                    name: 'status',
                    align: 'center',
                    formatter: function (cellValue, options, rowObject) {
                        return cellValue == 0 ? '未激活' : '已激活';
                    },
                },
                {
                    label: '激活时间',
                    name: 'active_time',
                    align: 'center',
                    formatter: function (cellValue, options, rowObject) {
                        return commonUtil.getTimeString(cellValue);
                    },
                },
                {
                    label: '用户',
                    name: 'status',
                    align: 'center',
                    formatter: function (cellValue, options, rowObject) {
                        return decodeURIComponent((rowObject.app_user || '') && rowObject.app_user.account);
                    },
                },

            ],
            styleUI: 'Bootstrap',
            datatype: 'json',
            url: urls.list,
            rowNum: 10,
            rowList: [10, 20, 30],
            viewrecords: true, // show the current page, data rang and total records on the toolbar
            height: true,
            postData: {
                type: 1
            },
            fitColumns: true,
            width: true,
            autowidth: true,
            shrinkToFit: true,
            ajaxGridOptions: {
                dataFilter: function (ret) {
                    var retObj = JSON.parse(ret);
                    if (retObj.status == 0) {
                        return JSON.stringify(retObj.data);
                    } else {
                        commonUtil.toast(retObj.msg);
                    }
                }
            },
            pager: "#app_coupon_discount_list_grid_pager"
        });
        // 初始化宽宽，用于自适应宽度
        $("#app_coupon_discount_list_grid").setGridWidth($("#page-wrapper").width(), true);
    }


    exports.init = init;
});